/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import ejb.BusinessLogicLocal;
import entity.Campo;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "ServletPortaleCampi", urlPatterns = {"/ServletPortaleCampi"})
public class ServletPortaleCampi extends HttpServlet {

    @EJB
    private BusinessLogicLocal businessLogic;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //List<String> listaCampi = businessLogic.getAllSportType();
        Path webAppPath = Paths.get(getServletContext().getRealPath("/"));
        Path root = webAppPath.getRoot();

        String subPath = root.toString().concat((webAppPath.subpath(0, webAppPath.getNameCount() - 4)).toString());
        subPath += "/mfm-centro-sportivo-war/web/JQuery/config.js";
        if(!(new File(subPath + "/mfm-centro-sportivo-war/web/JQuery/config.js").exists()))
        {
            businessLogic.updateFastSearch(subPath);
        }
        List<Campo> allCamps = businessLogic.getCampi();
        request.setAttribute("allCamps", allCamps);
        String op = request.getParameter("op");
        if(op == null){
            op="";
        }
            if (op.equals("ricerca")) {
                String sport = request.getParameter("sport");
                String campo = request.getParameter("campo");
                String orario = request.getParameter("orario");
                if(!sport.equals("") && !campo.equals("") && !orario.equals("")){
                    List<Campo> listaCampi = businessLogic.getCampiDisponibili(sport, campo, orario);
                    RequestDispatcher rd = getServletContext().getRequestDispatcher("/showCamp.jsp");
                    request.setAttribute("listacampi", listaCampi);
                    request.setAttribute("sport", sport);
                    request.setAttribute("campo", campo);
                    request.setAttribute("orario", orario);
                    request.setAttribute("op", "ricerca");
                    rd.forward(request, response); 
                }
                    //List<Campo> lista = businessLogic.getCampiBySport(sport);                    
                    /*if(!listaCampi.isEmpty()){     
                        out.println("<h3 style=\"margin-left:25px;\">Campi da "+ sport +" in " + campo +" disponibili alle ore "+ orario+" :</h3>\n");
                        out.println("<form name=\"fastSearch\">\n" +
                                    "   <table border=\"0\" align=\"center\">");   
                        int i = 0;
                        out.println("   <tr>\n");
                        for(Campo item : listaCampi){
                            if(i < 3){
                                out.println(
                                            "       <td>\n" +
                                            "       <img class=\"imageborder\" src=\""+item.getImmagine() +"\" width=\"200\" heigth=\"200\">" +                                                    
                                            "       <h3 align=\"center\">Campo numero: "+item.getNumeroCampo()+"</h3>"+
                                            "       <td>\n");                                            
                            }
                            i++;
                        }
                        out.println("       </tr>\n"+
                                    "    </table>\n"+
                                    "</form>\n");
                    }  
                    else
                        out.println("<h3 style=\"margin-left:25px;\">Siamo spiacenti al momento non ci sono campi disponibili!</h3>\n");
                }*/
            } else {
                    RequestDispatcher rd = getServletContext().getRequestDispatcher("/showCamp.jsp");
                    request.setAttribute("op", "other");
                    rd.forward(request, response); 
                /*out.println("<body onload=\"initListGroup('chainedmenu', document.listmenu0.firstlevel, document.listmenu0.secondlevel, document.listmenu0.thirdlevel, 'saveit')\">\n"
                        + "         <div class=\"main\">\n"
                        + "            <div class=\"page\">\n"
                        + "                <div class=\"header\">\n"
                        + "                    <div class=\"login\">\n");
                request.getRequestDispatcher("/login.jsp").include(request, response);
                out.println("                </div>\n"
                        + "                    <div class=\"banner\">\n"
                        + "                    </div>\n"
                        + "                    <div class=\"topmenu\">\n");
                request.getRequestDispatcher("/topmenu.jsp").include(request, response);
                out.println("                </div>\n"
                        + "                </div>\n"
                        + "                <div class=\"content\">\n"
                        + "                    <div class=\"content-in\">\n"
                        + "                        <div class=\"left-panel\">\n"
                        + "                            <div class=\"left-panel-in\">\n"
                        +"                                  <map id=\"mappacampi\" name=\"mappacampi\">\n" +
"                                                               <area shape=\"rect\" alt=\"\" title=\"\" coords=\"64,49,171,121\" href=\"tennisErbasinistra\"/>\n" +
"                                                               <area shape=\"rect\" alt=\"\" title=\"\" coords=\"234,49,343,121\" href=\"tennisErbadestra\"/>\n" +
"                                                               <area shape=\"rect\" alt=\"\" title=\"\" coords=\"447,11,577,96\" href=\"basketparquet\"/>\n" +
"                                                               <area shape=\"rect\" alt=\"\" title=\"\" coords=\"448,100,576,182\" href=\"basketlinoleum\"/>\n" +
"                                                               <area shape=\"rect\" alt=\"\" title=\"\" coords=\"520,195,633,278\" href=\"calcettoparquet\"/>\n" +
"                                                               <area shape=\"rect\" alt=\"\" title=\"\" coords=\"290,207,405,279\" href=\"tennisterra\"/>\n" +
                                                    "            <area shape=\"rect\" alt=\"\" title=\"\" coords=\"503,354,625,429\" href=\"pallavololinoleum\"/>\n" +
                                                    "            <area shape=\"rect\" alt=\"\" title=\"\" coords=\"251,332,336,447\" href=\"calcettolinoleum\"/>\n" +
                                                    "            <area shape=\"rect\" alt=\"\" title=\"\" coords=\"95,334,177,449\" href=\"calcettoerbadestra\"/>\n" +
                                                    "            <area shape=\"rect\" alt=\"\" title=\"\" coords=\"2,334,81,447\" href=\"calcettoervasinistra\"/>\n" +
                                                    "        </map>\n" +
                                                    "        <img class=\"imageborder\" style=\"margin-left:50px;\" src=\"Images/mappacampi.jpg\" alt=\"mappacampi\" usemap=\"#mappacampi\" width=\"640\" height=\"480\">\n"
                        + "                                <section class=\"insertModule\">\n"                       
                        + "                                   <h1 align=\"left\">Verifica immediata disponibilità:</h1>\n"
                        + "                                   <form name=\"listmenu0\">\n"
                        + "                                       <table border=\"0\" align=\"center\">\n"
                        + "                                           <tr>\n"
                        + "                                               <th width=\"230px\" scope=\"col\">\n"
                        + "                                                   <label>\n"
                        + "                                                       <div align=\"left\">Tipologia Campo:</div>\n"
                        + "                                                   </label>\n"
                        + "                                                   <div align=\"left\">\n"
                        + "                                                       <select name=\"firstlevel\" id=\"tipoCampo\" style=\"width:180px;\"></select>\n"
                        + "                                                   </div>\n"
                        + "                                               </th>\n"
                        + "                                               <th width=\"230px\" scope=\"col\">\n"
                        + "                                                   <label>\n"
                        + "                                                       <div align=\"left\">Tipologia Terreno:</div>\n"
                        + "                                                   </label>\n"
                        + "                                                   <div align=\"left\">\n"
                        + "                                                       <select name=\"secondlevel\" id=\"terrenoCampo\" style=\"width:180px;\"></select>\n"
                        + "                                                   </div>\n"
                        + "                                               </th>\n"
                        + "                                               <th width=\"230px\" scope=\"col\">\n"
                        + "                                                   <label>\n"
                        + "                                                       <div align=\"left\">Orario Prenotazione:</div>\n"
                        + "                                                   </label>\n"
                        + "                                                   <div align=\"left\">\n"
                        + "                                                       <select name=\"thirdlevel\" id=\"orarioCampo\"style=\"width:180px;\"></select>\n"
                        + "                                                       </select>"
                        + "                                                   </div>\n"
                        + "                                               </th>\n"
                        + "                                            </tr>\n"
                        + "                                            <tr>\n"
                        + "                                               <th colspan=\"3\" align=\"right\">"
                        + "                                                   <input class=\"btnsubmit\" type=\"button\" value=\"Ricerca\" onclick=\"call(thirdlevel.value)\">\n"                        
                        + "                                               </th>"
                        + "                                            </tr>\n"
                        + "                                       </table>\n"
                        + "                                   </form>\n"
                        + "                               </section>\n");
                out.println("<div id=\"txtHint\"></div>");
           
            out.println("                       </div>\n"
                    + "                        </div>\n"
                    + "                        <div class=\"right-panel\">\n"
                    + "                            <div class=\"right-panel-in\">\n");
            request.getRequestDispatcher("/right-content.jsp").include(request, response);
            out.println("                      </div>\n"
                    + "                        </div>\n"
                    + "                    </div>\n"
                    + "                </div>\n"
                    + "                <div class=\"footer\">\n");
            request.getRequestDispatcher("/footer.jsp").include(request, response);
            out.println("            </div>\n"
                    + "            </div>\n"
                    + "        </div> \n"
                    + "    </body>");*/
        }
}
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
